<?php
/**
 * 优惠券信息
 * @author 戚银
 * @email thinkercode@sina.com
 * @date 2015年8月24日 下午3:30:36
 */
class CouponsApp extends MallbaseApp {

    /**
     * 我的优惠券列表
     * @author jyx
     * @date 2015/10/23
     */
    public function index() {
        $action = '我的优惠券列表';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if (!$user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
        }
        $model = m('coupons');
        $data = array();
        $time = time();
        //获取我所有的优惠券列表
        $sql = "SELECT cp.id,co.amount,co.end_time,cp.status
                FROM ".DB_PREFIX."coupons_participate cp
                LEFT JOIN ".DB_PREFIX."coupons co ON cp.cid=co.id
                LEFT JOIN ".DB_PREFIX."activity a ON cp.aid=a.id
                LEFT JOIN ".DB_PREFIX."activity_product ap ON cp.aid= ap.aid
                WHERE cp.uid={$user_id} AND cp.status=0 AND co.status=1 AND a.status=1
                AND co.end_time>{$time} AND co.end_time>{$time} GROUP BY cp.id ORDER BY co.amount DESC,cp.add_time DESC";
        $tmp = $model->getAll($sql);
        if ($tmp) {
            foreach ($tmp as $key=>$val) {
                $val['type'] = 1;
                $val['range'] = 1;
                $tmp[$key] = $val;
            }
            $data = $tmp;
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }

    /**
     * 1.2版本正在使用
     * 我的优惠券列表
     * @author jyx
     * @date 2015/10/23
     */
	public function mycoupon(){
        $action = '我的优惠券列表';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
		$user_id = $this->checkLogin();
		if (!$user_id) {
			$this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
		}
		$model = m('coupons');
        $data = array();
        $time = time();
        //获取我所有的优惠券列表
        $sql = "SELECT cp.id,co.amount,co.end_time,cp.status
                FROM ".DB_PREFIX."coupons_participate cp
                LEFT JOIN ".DB_PREFIX."coupons co ON cp.cid=co.id
                LEFT JOIN ".DB_PREFIX."activity a ON cp.aid=a.id
                LEFT JOIN ".DB_PREFIX."activity_product ap ON cp.aid= ap.aid
                WHERE cp.uid={$user_id} AND cp.status=0 AND co.status=1 AND a.status=1
                AND co.end_time>{$time} AND co.end_time>{$time} GROUP BY cp.id ORDER BY co.amount DESC,cp.add_time DESC";
        $data = $model->getAll($sql);
        if ($data) {
            foreach ($data as $key=>$val) {
                $val['type'] = 1;
                $val['range'] = 1;
                $data[$key] = $val;
            }
            $this->ajax_return(FLAG_OK, '', $data);
        } else {
            $this->ajax_return(FLAG_OK, '优惠券列表为空！');
        }
	}

    /**
     * 1.3版本使用
     * 选择优惠券
     * @author jyx
     * @date 2015/10/23
     */
    public function chooseCoupons() {
        $action = '选择优惠券';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if (!$user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
        }

        $goods_id = $this->get_gp('good_id', 'G');
        if(!$goods_id){
            $this->ajax_return(FLAG_ERROR, '商品id不存在！');
        }

        $model = m('coupons');
        $data = array();
        $time = time();
        //获取我对应商品的优惠券列表
        $sql = "SELECT cp.id,co.amount,co.end_time,cp.status
                FROM ".DB_PREFIX."coupons_participate cp
                LEFT JOIN ".DB_PREFIX."coupons co ON cp.cid=co.id
                LEFT JOIN ".DB_PREFIX."activity a ON cp.aid=a.id
                LEFT JOIN ".DB_PREFIX."activity_product ap ON cp.aid= ap.aid
                WHERE ap.pid IN ({$goods_id}) AND cp.uid={$user_id} AND cp.status=0 AND co.status=1 AND a.status=1
                AND co.end_time>{$time} AND co.end_time>{$time} GROUP BY cp.id ORDER BY co.amount DESC,cp.add_time DESC";
        $tmp = $model->getAll($sql);
        if ($tmp) {
            foreach ($tmp as $key=>$val) {
                $val['type'] = 1;
                $val['range'] = 1;
                $tmp[$key] = $val;
            }
            $data = $tmp;
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }
    /**
     * 1.2版本
     * 选择优惠券
     * @author jyx
     * @date 2015/10/23
     */
	public function choose_coupon(){
        $action = '选择优惠券';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
		$user_id = $this->checkLogin();
		if (!$user_id) {
			$this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
		}

		$goods_id = $this->get_gp('good_id', 'G');
		if(!$goods_id){
			$this->ajax_return(FLAG_ERROR, '商品id不存在！');
		}

		$model = m('coupons');
        $data = array();
		$time = time();
        //获取我对应商品的优惠券列表
        $sql = "SELECT cp.id,co.amount,co.end_time,cp.status,s.store_name
                FROM ".DB_PREFIX."coupons_participate cp
                LEFT JOIN ".DB_PREFIX."coupons co ON cp.cid=co.id
                LEFT JOIN ".DB_PREFIX."activity a ON cp.aid=a.id
                LEFT JOIN ".DB_PREFIX."activity_product ap ON cp.aid= ap.aid
                LEFT JOIN ".DB_PREFIX."goods g ON ap.pid=g.goods_id
                LEFT JOIN ".DB_PREFIX."store s ON s.store_id=g.store_id
                WHERE ap.pid IN ({$goods_id}) AND cp.uid={$user_id} AND ap.pid={$goods_id} AND cp.status=0 AND co.status=1 AND a.status=1
                AND co.end_time>{$time} AND co.end_time>{$time} ORDER BY co.amount DESC,cp.add_time DESC";
        $data = $model->getAll($sql);
        if ($data) {
            foreach ($data as $key=>$val) {
                $val['type'] = 1;
                $val['range'] = 1;
                $data[$key] = $val;
            }
            $this->ajax_return(FLAG_OK, '', $data);
        } else {
            $this->ajax_return(FLAG_OK, '没有可用的优惠券！');
        }

	}

    /**
     * 1.3版本使用
     * 最合适的优惠券
     * @author jyx
     * @date 2015/10/23
     */
    public function optimalCoupons() {
        $action = '最合适的优惠券';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
        $user_id = $this->checkLogin();
        if (!$user_id) {
            $this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
        }

        $goods_id = $this->get_gp('good_id', 'G');
        if(!$goods_id){
            $this->ajax_return(FLAG_ERROR, '错误请求');
        }

        $model = m('coupons');
        $data = array();
        $time = time();
        //获取我对应商品的所有的优惠券列表中最优的优惠券
        $sql = "SELECT cp.id,co.amount,co.end_time,cp.status,s.store_name
                FROM ".DB_PREFIX."coupons_participate cp
                LEFT JOIN ".DB_PREFIX."coupons co ON cp.cid=co.id
                LEFT JOIN ".DB_PREFIX."activity a ON cp.aid=a.id
                LEFT JOIN ".DB_PREFIX."activity_product ap ON cp.aid= ap.aid
                LEFT JOIN ".DB_PREFIX."goods g ON ap.pid=g.goods_id
                LEFT JOIN ".DB_PREFIX."store s ON s.store_id=g.store_id
                WHERE ap.pid IN ({$goods_id}) AND cp.uid={$user_id} AND cp.status=0 AND co.status=1 AND a.status=1
                AND co.end_time>{$time} AND co.end_time>{$time} ORDER BY co.amount DESC,cp.add_time DESC LIMIT 1";
        $tmp = $model->getRow($sql);
        if ($tmp) {
            $data = $tmp;
            $data['type'] = 1;
            $data['range'] = 1;
        }
        $this->ajax_return(FLAG_OK, '', $data);
    }

    /**
     * 1.2版本使用
     * 最合适的优惠券
     * @author jyx
     * @date 2015/10/23
     */
	public function optimal_coupon(){
        $action = '最合适的优惠券';
        setLogParameter($action);
        $method = strtoupper($_SERVER['REQUEST_METHOD']);
        if ($method != 'GET') {
            $this->ajax_return(FLAG_ERROR, '非法操作！');
        }
		$user_id = $this->checkLogin();
		if (!$user_id) {
			$this->ajax_return(ERROR_LOGIN, '请您进行登陆！');
		}

		$goods_id = $this->get_gp('good_id', 'G');
		if(!$goods_id){
			$this->ajax_return(FLAG_ERROR, '错误请求');
		}

		$model = m('coupons');
        $data = array();
        $time = time();
        //获取我对应商品的所有的优惠券列表中最优的优惠券
        $sql = "SELECT cp.id,co.amount,co.end_time,cp.status,s.store_name
                FROM ".DB_PREFIX."coupons_participate cp
                LEFT JOIN ".DB_PREFIX."coupons co ON cp.cid=co.id
                LEFT JOIN ".DB_PREFIX."activity a ON cp.aid=a.id
                LEFT JOIN ".DB_PREFIX."activity_product ap ON cp.aid= ap.aid
                LEFT JOIN ".DB_PREFIX."goods g ON ap.pid=g.goods_id
                LEFT JOIN ".DB_PREFIX."store s ON s.store_id=g.store_id
                WHERE cp.uid={$user_id} AND ap.pid={$goods_id} AND cp.status=0 AND co.status=1 AND a.status=1
                AND co.end_time>{$time} AND co.end_time>{$time} ORDER BY co.amount DESC,cp.add_time DESC LIMIT 1";
        $data = $model->getRow($sql);
        if ($data) {
            $data['type'] = 1;
            $data['range'] = 1;
            $this->ajax_return(FLAG_OK, '', $data);
        } else {
            $this->ajax_return(FLAG_OK, '没有可用的优惠券！');
        }

	}
}